import { router } from "@/router/index";

export const usePermission = () => {
  const widgets = router.currentRoute.value.meta.widgets || [];
  const hasPermission = (permissions: string | string[]) => {
    let permissionList: string[] = [];
    if (Array.isArray(permissions)) {
      permissionList = permissions;
    } else if (typeof permissions === "string") {
      permissionList = permissions.split(",");
    } else {
      window.$message.warning("请传入权限标识");
    }
    return widgets.filter((item) => permissionList.indexOf(item.code) > -1).length;
  };

  return {
    hasPermission,
  };
};
